package org.oschina.api.upload.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.upload.model.UploadConfig;
import org.oschina.api.upload.service.UploadConfigService;
import org.oschina.api.upload.service.UploadRecordService;
import org.oschina.api.upload.vo.UploadConfigVo;
import org.oschina.api.upload.vo.UploadRecordVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/upload/config")
public class UploadConfigController extends BaseController {

	@Autowired
	private UploadConfigService uploadConfigService;
	@Autowired
	private UploadRecordService uploadRecordService;
	
	/**
	 * 返回配置
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<UploadConfigVo> findVo()
	{
		return uploadConfigService.findVo();
	}
	
	/**
	 * 更新
	 * @param uploadConfig
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="path",filter={TextOnlyFilter.class}),
			@ValidateField(name="size",validateValue=true,min=1),
			@ValidateField(name="type",filter={TextOnlyFilter.class})
		}) UploadConfig uploadConfig
	)
	{
		return uploadConfigService.update(uploadConfig);
	}
	
	/**
	 * 返回记录
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/record",method=RequestMethod.GET)
	public Pager<UploadRecordVo> findRecord(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return uploadRecordService.findVo(pn);
	}
}